package zzs.generalframework.console.core.parameter.commonparameter.nationality.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.nationality.NationalityBO;
import zzs.generalframework.common.core.parameter.commonparameter.nationality.NationalityInfo;
import zzs.generalframework.common.core.parameter.commonparameter.nationality.NationalityPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.nationality.dao.INationalityDao;
import zzs.generalframework.console.core.parameter.commonparameter.nationality.service.INationalityService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.nationality.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.nationality.service.impl.nationalityServiceImpl")
public class NationalityServiceImpl implements INationalityService
{

	private Logger logger = LoggerFactory.getLogger(NationalityServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	INationalityDao nationalityDao;

	@Override
	public ConsoleReturnBO saveNationality(NationalityBO nationalityBO)
	{
		logger.debug("====保存民族信息开始====");
		logger.debug("====保存民族信息.入参:NationalityBO=[{}]====", nationalityBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		NationalityPO nationalityPO = new NationalityPO();
		copyNationalityBO2NationalityPO(nationalityBO, nationalityPO);
		Boolean insertResult = nationalityDao.insert(nationalityPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存民族信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存民族信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveNationality(List<NationalityBO> nationalityBOList) throws ConsoleException
	{
		logger.info("====保存民族信息列表开始====");
		logger.debug("====保存民族信息列表.入参:NationalityBOList=[{}]====", nationalityBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (NationalityBO itemBO : nationalityBOList)
		{
			ConsoleReturnBO itemResult = saveNationality(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存民族信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存民族信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getNationality(NationalityInfo nationalityInfo)
	{
		logger.info("====获取民族信息开始====");
		logger.debug("====获取民族信息.入参:NationalityInfo=[{}]====", nationalityInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<NationalityPO> nationalityPOList = nationalityDao.select(nationalityInfo);
		if (nationalityPOList == null || nationalityPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (nationalityPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			NationalityBO nationalityBO = new NationalityBO();
			copyNationalityPO2NationalityBO(nationalityPOList.get(0), nationalityBO);
			returnConsoleReturnBO.setReturnObj(nationalityBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			NationalityBO nationalityBO = new NationalityBO();
			copyNationalityPO2NationalityBO(nationalityPOList.get(0), nationalityBO);
			returnConsoleReturnBO.setReturnObj(nationalityBO);
		}
		logger.debug("====获取民族信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取民族信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listNationality(NationalityInfo nationalityInfo)
	{
		logger.info("====获取民族信息列表开始====");
		logger.debug("====获取民族信息列表.入参:NationalityInfo=[{}]====", nationalityInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<NationalityPO> nationalityPOList = nationalityDao.select(nationalityInfo);
		if (nationalityPOList == null || nationalityPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<NationalityBO> resultList = new ArrayList<NationalityBO>();
			for (NationalityPO itemPO : nationalityPOList)
			{
				NationalityBO itemBO = new NationalityBO();
				copyNationalityPO2NationalityBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取民族信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取民族信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateNationality(NationalityBO nationalityBO)
	{
		logger.info("====更新民族信息开始====");
		logger.debug("====更新民族信息.入参:NationalityBO=[{}]====", nationalityBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		NationalityPO nationalityPO = new NationalityPO();
		copyNationalityBO2NationalityPO(nationalityBO, nationalityPO);
		Boolean updateResult = nationalityDao.update(nationalityPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新民族信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新民族信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateNationality(List<NationalityBO> nationalityBOList) throws ConsoleException
	{
		logger.info("====更新民族信息列表开始====");
		logger.debug("====更新民族信息列表.入参:List<NationalityBO>=[{}]====", nationalityBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (NationalityBO itemBO : nationalityBOList)
		{
			ConsoleReturnBO itemResult = updateNationality(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新民族信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新民族信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeNationality(NationalityBO nationalityBO)
	{
		logger.info("====删除民族信息开始====");
		logger.debug("====删除民族信息.入参:NationalityBO＝[{}]======", nationalityBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		NationalityPO nationalityPO = new NationalityPO();
		copyNationalityBO2NationalityPO(nationalityBO, nationalityPO);
		Boolean deleteResult = nationalityDao.delete(nationalityPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除民族信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除民族信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeNationality(List<NationalityBO> nationalityBOList) throws ConsoleException
	{
		logger.info("====删除民族信息列表开始====");
		logger.debug("====删除民族信息列表.入参:List<NationalityBO>＝[{}]======", nationalityBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (NationalityBO itemBO : nationalityBOList)
		{
			ConsoleReturnBO itemResult = removeNationality(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除民族信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除民族信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countNationality(NationalityBO nationalityBO)
	{
		logger.info("====统计民族信息开始====");
		logger.debug("====统计民族信息。入参：NationalityBO＝[{}]======", nationalityBO);
		Long returnLong = new Long(0);
		NationalityPO nationalityPO = new NationalityPO();
		copyNationalityBO2NationalityPO(nationalityBO, nationalityPO);
		returnLong = nationalityDao.count(nationalityPO);
		logger.debug("====统计民族信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计民族信息结束====");
		return returnLong;
	}

	private void copyNationalityBO2NationalityPO(NationalityBO nationalityBO, NationalityPO nationalityPO)
	{
		beanUtils.copyBeanNotNull(nationalityBO, nationalityPO);
		if (nationalityBO.getSysID() != null)
		{
			nationalityPO.setSeqID(nationalityBO.getSysID());
		}
	}

	private void copyNationalityPO2NationalityBO(NationalityPO nationalityPO, NationalityBO nationalityBO)
	{
		beanUtils.copyBeanNotNull(nationalityPO, nationalityBO);
		if (nationalityPO.getSeqID() != null)
		{
			nationalityBO.setSysID(nationalityPO.getSeqID());
		}
	}

}
